import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;

import java.util.ArrayList;
import java.util.List;


public class ExcelImport<T> implements ReadListener<T> {
    private DormitoryManager dormitoryManager;

    public ExcelImport(DormitoryManager dormitoryManager) {
        this.dormitoryManager = dormitoryManager;
    }

    private List<T> cacheList = new ArrayList<>();

    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        cacheList.add(t);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (cacheList.size() > 0) {
            for (T t : cacheList) {
                Student student = (Student) t;
                dormitoryManager.addStudent(student.getName(), student.getGender(),
                        student.getDormitoryNumber(), student.getBedNumber());
            }
        }
    }
}
